import { defineStore } from 'pinia'

import server from '@/config/server/index.js'

let api = {
  number: server.api().get('/api/property/isExistReject'),
  read: server.api().get('/api/property/closeRejectFlag')
}

export const messageStore = defineStore('message', {
  state: () => {
    return {
      number: 0,
      init: false
    }
  },
  actions: {
    show(update) {
      if (this.init && !update) {
        if (this.number) uni.showTabBarRedDot({ index: 2 })
        else uni.hideTabBarRedDot({ index: 2 })
      } else {
        this.update().then((res) => {
          this.init = true
          this.show()
        })
      }
    },
    update() {
      return api.number.fetch().then((res) => (this.number = res))
    },
    read(propertyId) {
      api.read.fetch({ propertyId }).then((res) => {
        this.update()
      })
    }
  }
})
